package MyGreed;

import java.util.Arrays;
import java.util.Comparator;

public class Leetcode0435 {
    public static void Print2(int[][] intervals) {
        for (int i = 0; i < intervals.length; ++i) {
            for (int j = 0; j < intervals[i].length; ++j) {
                System.out.print(intervals[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int eraseOverlapIntervals(int[][] intervals) {
        if (intervals.length == 0) {
            return 0;
        }
//        Arrays.sort(intervals, (o1, o2) -> o1[1] - o2[1]);
        Arrays.sort(intervals, new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });

        int count = 0;
        int right = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if(intervals[i][0] < right){
                count ++;
            }else{
                right = intervals[i][1];
            }
        }
        return count;

    }

    public static void main(String[] args) {
        int[][] arr = new int[][]{{1, 4}, {2, 3}, {3, 9}};
        eraseOverlapIntervals(arr);
    }
}
